package com.earthea.common.jackson;

import com.earthea.common.annotation.Anonymize;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import java.util.List;

public class AnonymizeBeanSerializerModifier extends BeanSerializerModifier {
    @Override
    public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {

        for (BeanPropertyWriter writer : beanProperties) {
            Anonymize anonymize = writer.getAnnotation(Anonymize.class);
            if (anonymize != null) {
                writer.assignSerializer(new AnonymizeSerializer(anonymize.type()));
            }
        }
        return beanProperties;
    }
}